package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.umsg.adapter.MyEmojiAdapter;
import com.uchengwang.ui.cm_ui.umsg.http.EnjoyChatManager;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import io.rong.imkit.emoticon.IEmojiItemClickListener;

/**
 * Created by U城网 on 2018/3/21.
 * 享聊第一次进入的界面
 */

public class EnjoyChatEnterActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private TextView enter_content, enter_tv;
    private ImageView enter_head, enter_emoje;
    private EditText enter_edit;
    private String editString;
    private FrameLayout enter_emoji_fl;
    private MyEmojiAdapter adapter;
    private View emojiView;
    private boolean isShow, isEmojiShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_enter);
        setTitle("享聊");

        initView();

    }

    private void initView(){
        enter_content = (TextView) findViewById(R.id.enter_content);
        enter_content.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (UCommonUtil.getUtil().isShowSoftInput(EnjoyChatEnterActivity.this)){
                    UCommonUtil.getUtil().hideSoftInput(EnjoyChatEnterActivity.this, getRy());
                }
                enter_emoji_fl.setVisibility(View.GONE);
                isEmojiShow = false;
                return false;
            }
        });
        enter_tv = (TextView) findViewById(R.id.enter_tv);
        enter_tv.setOnClickListener(this);
        enter_tv.setEnabled(false);
        enter_head = (ImageView) findViewById(R.id.enter_head);
        enter_emoje = (ImageView) findViewById(R.id.enter_emoje);
        enter_emoje.setOnClickListener(this);
        enter_edit = (EditText) findViewById(R.id.enter_edit);
        enter_edit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                enter_emoji_fl.setVisibility(View.GONE);
                isEmojiShow = false;
                return false;
            }
        });
        enter_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)){
                    enter_tv.setTextColor(getResources().getColor(R.color.register_user_selector));
                    enter_tv.setEnabled(true);

                }else {
                    enter_tv.setTextColor(getResources().getColor(R.color.comment_color));
                    enter_tv.setEnabled(false);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        enter_emoji_fl = (FrameLayout) findViewById(R.id.enter_emoji_fl);

        initData();
    }
    private void initData(){
        ImageManager.getInstance().loadImage(this, enter_head, UChengApplication.userAvatar, ImageManager.ImageType.CIRCLE);
        adapter = new MyEmojiAdapter();
        adapter.setOnItemClickListener(new IEmojiItemClickListener() {
            @Override
            public void onEmojiClick(String emoji) {
                int start = enter_edit.getSelectionStart();
                enter_edit.getText().insert(start, emoji);
            }

            @Override
            public void onDeleteClick() {
                enter_edit.dispatchKeyEvent(new KeyEvent(0, 67));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.enter_tv://点击进入
                editString = enter_edit.getText().toString();

                if (TextUtils.isEmpty(editString)){
                    UCommonUtil.getUtil().showToast(this, "一句好的开场白, 更能吸引人");
                    return;
                }

                MapRequest map = new MapRequest();
                map.put(BaseConfig.CONTENT, editString);
                map.put(BaseConfig.UNI_ID, UChengApplication.myUniId);
                map.put(BaseConfig.UNI_NAME, UChengApplication.mySchoolName);
                EnjoyChatManager.getManager().releaseChat(this, map, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            SPUtils.getPreferences(EnjoyChatEnterActivity.this).setEnterEnjoyChatTime();
                            startActivity(new Intent(EnjoyChatEnterActivity.this, EnjoyChatListActivity.class));
                            EnjoyChatEnterActivity.this.finish();
                        }else {
                            UCommonUtil.getUtil().showToast(EnjoyChatEnterActivity.this, msg);
                        }
                    }
                });
                break;

            case R.id.enter_emoje://点击表情
                isShow = UCommonUtil.getUtil().isShowSoftInput(this);//软键盘是否打开
                if (isShow){
                    //打开
                    UCommonUtil.getUtil().hideSoftInput(this, getRy());
                }
                if (isEmojiShow){
                    //emoji在表情打开 则关闭
                    enter_emoji_fl.setVisibility(View.GONE);
                    isEmojiShow = false;
                }else {
                    enter_emoji_fl.setVisibility(View.VISIBLE);
                    isEmojiShow = true;
                    //emoji表情关闭  则打开
                    if (emojiView == null){
                        emojiView = adapter.obtainTabPager(this);
                    }
                    enter_emoji_fl.removeAllViews();
                    enter_emoji_fl.addView(emojiView);
                }


                break;
        }
    }
}
